package com.black.platform.dictionary.service.bz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.common.constant.ShareConstant;
import com.black.common.web.PageQuery;
import com.black.platform.dictionary.mapper.DictionaryMapper;
import com.black.platform.dictionary.service.bz.DictionaryService;
import com.black.platform.dictionary.po.Dictionary;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Override
    public IPage<Dictionary> search(PageQuery<Dictionary> searchVo) {
        Dictionary queryData = searchVo.getQueryData();
        QueryWrapper<Dictionary> query = new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryData.getCodeType())) {
            query.eq("code_type", queryData.getCodeType());
        }
        if (!StringUtils.isEmpty(queryData.getValid())) {
            query.eq("valid", queryData.getValid());
        }
        if (!StringUtils.isEmpty(queryData.getCode())) {
            query.like("code", queryData.getCode());
        }
        if (!StringUtils.isEmpty(queryData.getName())) {
            query.like("name", queryData.getName());
        }
        return this.baseMapper.selectPage(searchVo.getPager(), query);
    }

    @Override
    public List<Dictionary> getAllValidDictionaryList() {
        QueryWrapper<Dictionary> query = new QueryWrapper<>();
        query.eq("valid", ShareConstant.Status.VALID);
        return this.baseMapper.selectList(query);
    }

    @Override
    public List<Dictionary> getByCodeType(String codeType, boolean isIncludeInvalid) {
        QueryWrapper<Dictionary> query = new QueryWrapper<>();
        if (!isIncludeInvalid) {
            query.eq("valid", ShareConstant.Status.VALID);
        }
        query.eq("code_type", codeType);
        return this.baseMapper.selectList(query);
    }

    @Override
    public List<Dictionary> getAllCodeType() {
        return this.baseMapper.getAllCodeType();
    }

}
